package com.dao;

import com.beans.MenuInfo;
import com.jdbc.DBUtil;
import java.util.List;

public class MenuDao {
    /**
     * 根据父级id查询所有子级
     * @param parentId 父级id，如果传的是0，则查询所有一级菜单
     * @return 菜单列表
     */
    public List<MenuInfo> getMenuList(int parentId) {
        List<MenuInfo> menuList = DBUtil.getList("select * from menuInfo where parentId=?", MenuInfo.class, parentId);

        for (MenuInfo m : menuList) {
            if (m.getParentId() == 0) {
                m.setSubMenuList(getMenuList(m.getId()));
            }
        }
        return menuList;
    }
    /**
     * 根据当前用的角色，查询用户拥有的菜单信息
     * @param parentId 父级id
     * @param roleId 角色id
     * @return 菜单列表
     */
    public List<MenuInfo> getMenuList(int parentId,int roleId){
        String sql = "select * from menuInfo where parentId=? and id in (select menuId from roleMenu where roleId=?)";
        List<MenuInfo> menuList = DBUtil.getList(sql,MenuInfo.class,parentId,roleId);
        for (MenuInfo m : menuList){
            if (m.getParentId()==0){
                m.setSubMenuList( getMenuList(m.getId(),roleId));
            }
        }
        return menuList;
    }
}
